package com.acme.bankapp.service.bank;


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.service.bank.commands.BankConsoleStatisticsCommand;
import com.acme.bankapp.service.bank.commands.BankConsoleStatisticsCommandFactory;
import com.acme.bankapp.service.network.BankRemoteConsole;
import com.acme.bankapp.service.network.BankServer;

/**
 * @author Artem Kalchenko
 * 
 */
public class SystemTest {

	private static Bank theBank = new Bank();
	private static BankService bs = new BankService();
	private static EmailService es = new EmailService();
	private static BankReporter br = new BankReporter();
	
	public static void main(String[] args) {
		theBank = bs.loadBankFromFeed("bank.feed");

		if (args.length == 0) {
			runMyTests();
			return;
		}
		
		if (args.length == 1) {
			if ("-statistics".equals(args[0])) {
				runConsoleStatistics();
				return;
			}
		}
	}

	private static void runConsoleStatistics() {
		String input;
		Map<String,BankConsoleStatisticsCommand> cmdMap = BankConsoleStatisticsCommandFactory.Create();
		BankConsoleStatisticsCommand inputCmd;
		
		System.out.println("Available commands:");
		for (Map.Entry<String, BankConsoleStatisticsCommand> cmd: cmdMap.entrySet()) {
			System.out.println(cmd.getKey() + " - " + cmd.getValue().getCommandDescription());
		}
		System.out.println("exit - exit console");
		
		do {
			input = System.console().readLine();
			if ((inputCmd = cmdMap.get(input)) != null) {
				System.out.println(inputCmd.execCommand(br, theBank));
			} else {
				System.out.println("Unknown command");
			}
		} while (!"exit".equals(input));
	}

	private static void runMyTests() {
		Client clSmith = new Client("Smith", Gender.MALE);
		Client clLiskov = new Client("Liskov", Gender.FEMALE,
				new CheckingAccount(500, 100));
		Client clSmithCopy = null;

		bs.addClient(theBank, clLiskov);
		bs.addClient(theBank, clSmith);
		bs.addAccount(theBank, clSmith, new SavingsAccount(300));
		bs.addAccount(theBank, clSmith, new CheckingAccount(300,100));
		
		System.out.println(br.getNbOfCustomers(theBank));
		System.out.println(br.getNbOfAccounts(theBank));
		System.out.println(br.getBankCredit(theBank));
		System.out.println(br.getAccountsSortedBySum(theBank).toString());
		bs.withdraw(theBank, clLiskov,clLiskov.getAccounts().get(0), 550);
		System.out.println(br.getBankCredit(theBank));
		
		System.out.println(br.getAccountsSortedBySum(theBank).toString());
	}
}
